package com.example.llm.learn.api.controller;

import com.example.llm.learn.api.dto.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin(origins = {"http://localhost:5173", "http://127.0.0.1:5500"})
@RestController
public class SPUController implements InitializingBean {

    private List<SPUDTO> list = new LinkedList<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        SPUDTO spudto1 = new SPUDTO().setId(1L)
                .setName("华为")
                .setDescription("华为创立于1987年，是全球领先的ICT（信息与通信）基础设施和智能终端提供商。华为目前有20.8万员工，业务遍及170多个国家和地区，服务全球30多亿人口。\n" +
                        "\n" +
                        "华为致力于把数字世界带入每个人、每个家庭、每个组织，构建万物互联的智能世界：让无处不在的联接，成为人人平等的权利，成为智能世界的前提和基础；为世界提供多样性算力，让云无处不在，让智能无所不及；所有的行业和组织，因强大的数字平台而变得敏捷、高效、生机勃勃；通过AI重新定义体验，让消费者在家居、出行、办公、影音娱乐、运动健康等全场景获得极致的个性化智慧体验。")
                .setAttrId(111L)
                .setTradeMarkId(1L)
                .setTradeMarkName("华为")
                .setImageList(Arrays.asList("https://consumer.huawei.com/content/dam/huawei-cbg-site/cn/mkt/pdp/phones/mate60-pro/img/kv/kv-left@2x.webp",
                        "https://consumer.huawei.com/content/dam/huawei-cbg-site/cn/mkt/pdp/phones/mate60-pro/img/kv/kv-right@2x.webp",
                        "https://consumer.huawei.com/content/dam/huawei-cbg-site/cn/mkt/pdp/phones/mate60-pro/img/kv/sellpoint-2@2x.webp",
                        "https://consumer.huawei.com/content/dam/huawei-cbg-site/cn/mkt/pdp/phones/mate60-pro/img/camera/camera-intro@2x.webp",
                        "https://consumer.huawei.com/content/dam/huawei-cbg-site/cn/mkt/pdp/phones/mate60-pro/img/ark/ark-battery@2x.webp",
                        "https://consumer.huawei.com/content/dam/huawei-cbg-site/cn/mkt/pdp/phones/mate60-pro/img/acs/accessories-case-3@2x.webp",
                        "https://consumer.huawei.com/content/dam/huawei-cbg-site/cn/mkt/pdp/phones/mate60-pro/img/acs/accessories-case-5@2x.webp"));
        List<AttrNameDTO> attrNameList1 = new ArrayList<>();
        List<AttrValueDTO> valueList1 = new LinkedList<>();
        valueList1.add(new AttrValueDTO().setId(11111L).setName("安卓").setAttrNameId(1111L));
        valueList1.add(new AttrValueDTO().setId(11112L).setName("苹果").setAttrNameId(1111L));
        attrNameList1.add(new AttrNameDTO().setId(1111L).setName("系统").setAttrId(111L).setValueList(valueList1));
        List<AttrValueDTO> valueList2 = new LinkedList<>();
        valueList2.add(new AttrValueDTO().setId(11121L).setName("64G").setAttrNameId(1112L));
        valueList2.add(new AttrValueDTO().setId(11122L).setName("128G").setAttrNameId(1112L));
        valueList2.add(new AttrValueDTO().setId(11123L).setName("256G").setAttrNameId(1112L));
        attrNameList1.add(new AttrNameDTO().setId(1112L).setName("内存").setAttrId(111L).setValueList(valueList2));
        spudto1.setAttrNameList(attrNameList1);
        list.add(spudto1);
    }

    @GetMapping(value = "/spu/list")
    public CommonResponse<PageDTO<SPUDTO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "attrId", defaultValue = "10") Long attrId) {
        PageDTO<SPUDTO> page = new PageDTO<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        List<SPUDTO> filterList = list.stream().filter(item -> item.getAttrId().equals(attrId)).toList();
        page.setTotal(filterList.size());
        int skip = Math.min(filterList.size(), (pageNo - 1) * pageSize);
        page.setList(filterList.stream().skip(skip).limit(pageSize).collect(Collectors.toList()));
        return CommonResponse.ok(page);
    }

    @PostMapping(value = "/spu/add")
    public CommonResponse<Boolean> add(@RequestBody SPUDTO spudto) {
        spudto.setId(System.currentTimeMillis());
        list.add(spudto);
        return CommonResponse.ok(Boolean.TRUE);
    }

    @PostMapping(value = "/spu/edit")
    public CommonResponse<Boolean> edit(@RequestBody SPUDTO spudto) {
        SPUDTO tradeMark = findById(spudto.getId());
        if (Objects.isNull(tradeMark)) {
            return CommonResponse.error500("不存在的记录");
        }
        tradeMark.setName(spudto.getName());
        tradeMark.setDescription(spudto.getDescription());
        tradeMark.setTradeMarkId(spudto.getTradeMarkId());
        tradeMark.setTradeMarkName(spudto.getTradeMarkName());
        tradeMark.setImageList(spudto.getImageList());
        tradeMark.setAttrNameList(spudto.getAttrNameList());
        return CommonResponse.ok(Boolean.TRUE);
    }

    @DeleteMapping(value = "/spu/delete")
    public CommonResponse<Boolean> delete(@RequestParam Long id) {
        SPUDTO spudto = findById(id);
        if (Objects.isNull(spudto)) {
            return CommonResponse.error500("不存在的记录");
        }
        list = list.stream().filter(item -> !item.getId().equals(id)).collect(Collectors.toList());
        return CommonResponse.ok(Boolean.TRUE);
    }

    private SPUDTO findById(Long id) {
        Iterator<SPUDTO> iterator = list.iterator();
        while (iterator.hasNext()) {
            SPUDTO spudto = iterator.next();
            if (spudto.getId().equals(id)) {
                return spudto;
            }
        }
        return null;
    }
}
